﻿using UnityEngine;
using System;
using System.Collections;

public class Scene : RecieaveInputObject
{
    // 씬 베이스
    // BGM은 씬이 오디오 리스너를 가지고 있음

	GameObject UIRoot;
    public bool isHaveFadeIn = false;
    public bool isHaveFadeOut = false;
    public bool hasLeftTopMenu = false;
	public bool hasBackButton = false;
	// public bool hasHelpButton = false;

    public static Scene Instance;

    public string BGM = null;

    public override void AwakeObject()
    {
        Instance = this;
		UIRoot = GameObject.Find("UI Root");

		m_Cam = UIRoot.transform.Find("Camera").camera;
		SceneController.CrrSceneScript = this;

        if (isHaveFadeIn)
            FadeScreen.StartFadeIn();
        else
            FadeScreen.HideFadeScreen();

//		GameObject FPSDisplay = NGUITools.AddChild(UIRoot, Resources.Load ("Prefab/Common/Panel - FPS Display") as GameObject);
//		FPSDisplay.transform.localPosition = Vector3.zero;
//		FPSDisplay.transform.localScale = Vector3.one;

	    _ShowDiagnostic();	// by forour, for debugging

        AwakeScene();

		// 좌측 상단 메뉴를 포함해야 하는 Scene일 경우, 표시함
		if ( hasLeftTopMenu ) SceneController.ShowTopLeftMenu();
		if ( hasBackButton ) SceneController.ShowTopRightMenu();

		PostAwakeScene();

        if (BGM != null && BGM != "")
            SoundController.PlayBGM(BGM);
    }


	public void SetActiveHelpButton(bool isActive)
	{
		if (SceneController.m_TopRightMenu == null)
		{
			//Debug.LogError ("Top Right Menu is null"); return;
			SceneController.ShowTopRightMenu();
		}
		SceneController.m_TopRightMenu.GetComponent<Panel_TopRight>().helpButton.SetActive(isActive);
	}

    public virtual void AwakeScene()
    {

    }

	//TODO: 좌측상단바 처리를 위한 임시 코드 (추후 수정예정)
	public virtual void PostAwakeScene()
	{
		
	}

    // Use this for initialization
    public override void StartObject()
    {
		if(SceneController.SC != null)
        	SceneController.SC.SetLoadingActive(false);

        //isAbleControl = true;   // 씬 로딩후 터치 가능     
      
        StartScene();
    }

    public virtual void StartScene()
    {
    }


	public virtual void OnWillDestroy()
	{
	}


	// Update is called once per frame
    public override void UpdateObject()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            // 안드로이드 백버튼 처리 구역
            if(!PopupManager.IsPopupExist)
            {
                BackButton();
            }
            else
            {
                //PopupManager.PopPopup();
            }
        }

        UpdateScene();
    }

    public virtual void UpdateScene()
	{
    }

    public virtual void BackButton()
    {
    }

    public virtual void HelpButton()
    {
    }

	public virtual void HomeButton()
	{
		SceneController.LoadLevel("Main");
	}

    public virtual void ShopButton()
    {
    }


	#region Debugging

	private static bool _show_diagnostic;

	// Debuging용으로 FPS 출력을 껏다가 끄는 기능. by forour
	public static void ToggleDiagnostic()
	{
		_show_diagnostic = !_show_diagnostic;

		if( Instance )
			Instance._ShowDiagnostic();
	}


	private void _ShowDiagnostic()
	{
		if( _show_diagnostic )
		{
			var fps_monitor = gameObject.MakeComponent<FPSMonitor>();
			fps_monitor.enabled = true;
		}
		else
		{
			var fps_monitor = gameObject.GetComponent<FPSMonitor>();
			if( fps_monitor )
				fps_monitor.enabled = false;
		}
	}

	#endregion
}
